package com.wans.guli.search.config;

import org.apache.http.HttpHost;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.client.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticSearchConfig {
    @Value("${elasticsearch.host}")
    public String Host;
    @Value("${elasticsearch.port}")
    public Integer Port;
    @Value("${elasticsearch.scheme}")
    public String Scheme;

    // 设置esClient的默认参数
    public static final RequestOptions COMMON_OPTIONS;

    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();

        // 设置默认的请求参数 - 设置缓冲区大小为 50MB
        int bufferSize = 50 * 1024 * 1024;
        builder.setHttpAsyncResponseConsumerFactory(
                new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(bufferSize)
        );
        COMMON_OPTIONS = builder.build();
    }


    @Bean("esClient")
    public RestHighLevelClient restHighLevelClient() {
        RestClientBuilder builder = RestClient.builder(new HttpHost(Host, Port, Scheme));


        return new RestHighLevelClient(builder);
    }
}
